<template>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="5">
        <el-input
          placeholder="岗位名称"
          type="text"
          v-model="queryForm.t.jobName"
          clearable
        ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initGetList"
        >查询</el-button
      >
      <el-button type="primary" @click="handleDialogValue()">添加</el-button>
    </el-row>

    <el-divider border-style="dashed" />

    <el-table :data="tableData" stripe style="width: 100%; height: 100%">
      <el-table-column
        v-for="(item, index) in options"
        :prop="item.prop"
        :label="`${item.label}`"
        :key="index"
        :fixed="item.fixed"
        :width="item.width"
      >
        <template v-slot="{ row }" v-if="item.prop === 'createTime'">
          {{ $filters.filterUTCtimes(row.createTime) }}
        </template>

        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <el-button type="primary" size="small" @click="handleDialogValue(row)"
            >修改</el-button
          >
          <el-button type="danger" size="small" @click="delJob(row)"
            >删除</el-button
          >
          <el-button
            type="info"
            size="small"
            @click="handlePermDialogValue(row)"
            >分配权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="queryForm.num"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryForm.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @initList="initGetList"
    />
  </el-card>

  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    :dialogTableVal="dialogTableVal"
    @initList="initGetList"
  />
  <!-- 在v-if为false，也就是关闭弹框时即销毁Dialog中的内容，在下次显示时再重新加载 -->
  <PermDialog
    v-if="permDialogVisible"
    v-model="permDialogVisible"
    :dialogTitle="permDialogTitle"
    :dialogTableVal="permDialogTableVal"
    :allPermList="allPermList"
    @initList="initGetList"
  />
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { getJobPage, deleteJob } from '@/api/job.js'
import { listToTree } from '@/util/transfer.js'
import { getPermList } from '@/api/perm.js'
import { ref } from 'vue'
import { options } from './options'
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from './components/dialog.vue'
import PermDialog from './components/perm-dialog.vue'
const queryForm = ref({
  t: {
    jobName: ''
  },
  sortList: null,
  page: 1,
  size: 10
})
// 岗位
const total = ref(0)
const tableData = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const dialogTableVal = ref({})
// 权限
const permDialogVisible = ref(false)
const permDialogTitle = ref('')
const permDialogTableVal = ref({})
const allPermList = ref([])

// 分页查询岗位
const initGetList = async () => {
  const res = await getJobPage(queryForm.value)
  total.value = Number(res.total)
  tableData.value = res.dataList
}
initGetList()

// 查询权限菜单列表
const initGetAllPermList = async () => {
  const res = await getPermList({
    permType: '1'
  })
  allPermList.value = listToTree(res)
}
initGetAllPermList()

// 修改页大小
const handleSizeChange = (pageSize) => {
  queryForm.value.num = 1
  queryForm.value.size = pageSize
  initGetList()
}

// 修改页码
const handleCurrentChange = (page) => {
  queryForm.value.page = page
  initGetList()
}

const isNull = (obj) => {
  if (!obj) return true
  if (JSON.stringify(obj) === '{}') return true
  if (JSON.stringify(obj) === '[]') return true
}

// 更新是否显示对话框
const handleDialogValue = (row) => {
  if (isNull(row)) {
    dialogTitle.value = '添加岗位'
    dialogTableVal.value = {}
  } else {
    dialogTitle.value = '编辑岗位'
    dialogTableVal.value = row
  }
  dialogVisible.value = true
}

// 更新是否显示对话框
const handlePermDialogValue = (row) => {
  permDialogTitle.value = '分配权限'
  permDialogTableVal.value = JSON.parse(JSON.stringify(row))
  permDialogVisible.value = true
}

// 删除
const delJob = (row) => {
  ElMessageBox.confirm('确定要删除权限吗？', '确认提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await deleteJob(row.id)
      ElMessage({
        type: 'success',
        message: '删除成功'
      })
      initGetList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除失败'
      })
    })
}
</script>

<style lang="scss" scoped>
.el-col {
  margin-bottom: 16px;
}
.el-pagination {
  margin-top: 20px;
  // justify-content: center; //居中
  //float: left;居左
  float: right; //居右
}
:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background-color: #ff9800 !important; //修改默认的背景色
}
</style>
